import { PrismaClient } from '@prisma/client'
import bcrypt from 'bcrypt';

const prisma = new PrismaClient()
const saltRounds = 10;

async function testConnection() {
  try {
    const questions = await prisma.question.findMany()
    const questionType = await prisma.questionType.findMany()
    console.log('✅ 数据库连接成功! 题目数量:', questions.length)
    console.log('✅ 数据库连接成功! 题目类型数量:', questionType.length)
  } catch (error) {
    console.error('❌ 数据库连接失败:', error)
  } finally {
    await prisma.$disconnect()
  }
}

const testUsers = [
  { username: 'admin', password: 'Admin@123', role: 'ADMIN' },
  { username: 'teacher1', password: 'Teacher@123', role: 'TEACHER' },
  { username: 'student1', password: 'Student@123', role: 'STUDENT' }
];

async function createTestUsers() {
  for (const user of testUsers) {
    user.password = await bcrypt.hash(user.password, saltRounds);
    await prisma.user.create({ data: user });
  }
}

async function main() {
  await createTestUsers();
  await testConnection();
}

main().catch(e => {
  console.error(e);
  process.exit(1);
});